<?php
/**
 * Created by PhpStorm.
 * User: zhenghao
 * Date: 2015/6/16
 * Time: 16:03
 */


class MemberBO {

    //用户通过手机号码，密码注册
    static function registByPhoneAndPassword($phone,$pwd){

        //检查手机号码是否已经存在，如果存在，不能注册
        $is_exists=self::checkPhoneExists($phone);

        if($is_exists) throw new Exception("phone is exists by:".$phone);

        //注册用户
        $member=self::regist($phone,$pwd);

        return $member;
    }

    //会员通过手机号码，密码登录
    static function loginByPhoneAndPassword($phone,$pwd){

        //通过手机号码，密码获取用户信息
        $member=self::getMemberByPhoneAndPasswod($phone,$pwd);

        //更新用户最后登录时间
        self::updateLoginTime($member["id"]);

        return $member;
    }


    //更新指定会员最后登录时间
    private static function updateLoginTime($mid){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $str=array();
        $str["last_logintime"]=time();

        $where="id=".$mid;

        $db->update($table_name,$str,$where);
    }


    //通过手机号码、登陆密码获取会员信息
    //登陆规则，user_name 或 cellphone 存在相同的手机号码即认为手机号码正确
    private static function getMemberByPhoneAndPasswod($phone,$pwd){


        //分别检查 phone ， cellphone 是否匹配

        //1.检查 phone 、password 是否存在
        $member=self::checkMemberExistsByPhoneAndPassword($phone,$pwd);

        if(!empty($member)) return $member;


        //2.检查 cellphone 、password 是否存在
        $member=self::checkMemberExistsByCellPhoneAndPassword($phone,$pwd);

        if(!empty($member)){

            return $member;

        }else{

            throw new Exception("not found member info by phone:$phone and password:$pwd");
        }

    }


    private  static function checkMemberExistsByPhoneAndPassword($phone,$pwd){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $str=array();

        $str["fields"]="id,user_name,cellphone,power";
        $str['where']="user_name='$phone' and passwordmd5='".md5($pwd)."'";

        $members=$db->select($table_name,$str);

        if(empty($members)) return null;

        return $members[0];

    }


    private  static function checkMemberExistsByCellPhoneAndPassword($phone,$pwd){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $str=array();

        $str["fields"]="id,user_name,cellphone,power";
        $str['where']="cellphone='$phone' and passwordmd5='".md5($pwd)."'";

        $members=$db->select($table_name,$str);

        if(empty($members)) return null;

        return $members[0];
    }


    //会员重置密码
    static function resetPassword($phone,$new_pwd){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $str=array();

        $str["password"]=$new_pwd;
        $str["passwordmd5"]=md5($new_pwd);

        $where="user_name='$phone' or cellphone='$phone'";

        $db->update($table_name,$str,$where);

    }


    //检查手机号码是否存在。存在返回 true ，否则返回 false
    //检查规则：会员表中，user_name 或 cellphone 中有一个地方存在相同的手机号码即视为已存在
    static function checkPhoneExists($phone){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $str=array(
            "fields"    =>  "id",
            "where"     =>  "user_name='$phone' or cellphone='$phone'"
        );

        $result=$db->select($table_name,$str);

        return !empty($result);
    }


    //通过手机号码、密码，注册用户
    private static function regist($phone,$password){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."user_table";

        $member=array();
        $member["user_name"]=$phone;
        $member["cellphone"]=$phone;
        $member["language"]="te";
        $member["open_time"]=time();
        $member["user_ip"]=getIP();
        $member["password"]=$password;
        $member["passwordmd5"]=md5($password);
        $member["is_phone"]="1";

        //保存用户资料，并返回会员编号
        $mem_id=$db->insert($table_name,$member);

        $member["id"]=$mem_id;

        return $member;
    }




}